import { _decorator, Color, Component, EventTouch, instantiate, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Hero, LocationStrList, NumberStrList } from '../entity/Hero';
import { UIHeader } from '../common/UIHeader';
import { EventManager } from '../event/EventManager';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Skill } from '../entity/Skill';
import { SkillDesc } from './SkillDesc';
import { Strengthen } from './Strengthen';
import { Upgrade } from './Upgrade';
import { HeroList } from './HeroList';
const { ccclass, property } = _decorator;

@ccclass('HeroDetail')
export class HeroDetail extends Component {

    public static instance:HeroDetail

    @property(UIHeader)
    uiHeader:UIHeader
    @property(Sprite)
    cover:Sprite
    @property(Label)
    heroName:Label
    @property(Label)
    starStoneNum:Label
    @property(Label)
    upgradeStoneNum:Label
    @property(Label)
    level:Label
    @property(Label)
    advance:Label
    @property(Label)
    star:Label
    @property(Label)
    location:Label
    @property(Label)
    race:Label
    @property(Label)
    attack:Label
    @property(Label)
    defense:Label
    
    @property(Node)
    skillNode: Node
    @property(Node)
    skillLayout:Node
    @property(Node)
    strengthen:Node
    @property(Node)
    upgrade:Node
    @property(SpriteFrame)
    bg1:SpriteFrame
    @property(SpriteFrame)
    bg2:SpriteFrame

    private hero:Hero
   
    private skillNodeList:Node[] = []
    private skillList:Skill[] = []

    protected onLoad(): void {
        HeroDetail.instance = this
    }

    start() {
        this.uiHeader.setData('英雄详情')
        EventManager.instance.on('英雄详情close', this.onClose, this)

        this.strengthen.on(Node.EventType.TOUCH_END, this.onStrengthen, this)
        this.upgrade.on(Node.EventType.TOUCH_END, this.onUpgrade, this)
    }

    async setData(hero:Hero){
        console.log(hero)
        this.hero = hero
        this.cover.spriteFrame = await hero.getImage()
        this.heroName.string = hero.name
        this.starStoneNum.string = hero.p + ''
        this.upgradeStoneNum.string = HeroList.instance.chip + ''
        this.level.string = hero.l + '级'
        this.advance.string = NumberStrList[hero.ad] + '阶'
        this.star.string = hero.en + '星'
        this.location.string = LocationStrList[hero.loc]
        this.attack.string = hero.Attack + ''
        this.defense.string = hero.Defense + ''

        this.skillList = Skill.getListByHeroId(hero.id)
      

        this.skillNode.removeFromParent()
        this.skillLayout.destroyAllChildren()
        for(let i = 0; i< this.skillList.length; i++){
            let copy = instantiate(this.skillNode)
            this.skillLayout.addChild(copy)
            this.skillNodeList.push(copy)
            this.setSkill(copy, this.skillList[i], i)
        }

        if(hero.ad == 0){
            this.location.string = ''
            this.advance.string = '未激活'
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.HeroDetail)
    }

    setSkill(node:Node, skill:Skill, index:number){
        let adTitle:Label = node.getChildByName('AdTitle').getComponent(Label)
        let skillName:Label = node.getChildByName('SkillName').getComponent(Label)
        let bg:Sprite = node.getComponent(Sprite)
        let lock:Sprite = node.getChildByName('lock').getComponent(Sprite)
        
        adTitle.string = NumberStrList[index + 1] + '阶技能'
        skillName.string = skill.name

        if(this.hero.ad >= (index + 1)){
            lock.node.active = false
        } else {
            lock.node.active = true
            bg.spriteFrame = this.bg2
            adTitle.color = new Color('#A4A4A4')
            skillName.color = new Color('#A4A4A4')
        }

        node.on(Node.EventType.TOUCH_END, this.onClickSkill, this)
    }

    async onClickSkill(e:EventTouch){
        
        let skillName = e.currentTarget.getChildByName('SkillName').getComponent(Label).string
        for(let i=0; i < this.skillList.length; i++){
            let s = this.skillList[i]
            if(skillName == s.name){
                console.log(s)
                let ui = await UIManager.instance.openUI(PrefabEnum.SkillDesc)
                ui.getComponent(SkillDesc).setData(s, i + 1)
            }
        }
       
    }   

    async onStrengthen(){
        if(this.hero.ad == 0){
            UIManager.instance.tip('英雄未激活')
            return
        }

        let ui = await UIManager.instance.openUI(PrefabEnum.HeroStrengthen)
        ui.getComponent(Strengthen).setData(this.hero)
    }

    async onUpgrade(){
        if(this.hero.ad == 0){
            UIManager.instance.tip('英雄未激活')
            return
        }

        let ui = await UIManager.instance.openUI(PrefabEnum.HeroUpgrade)
        ui.getComponent(Upgrade).setData(this.hero)
    }
}


